// hdu3949
// 题解：
// 一样的题，另一种写法
//
// ml:run = $bin < input
#include<iostream>
#include<algorithm>

int const maxn = 10100;
using ll = long long;
ll a[maxn];
int n;
bool zero;

// for liner base
void gaussian_elimination()
{
    int k = 0;
    for (int j = 60; j >= 0; j--) {
        int i = k + 1;
        for (; i <= n; i++)
            if ((a[i] >> j) & 1) break;
        if (i == n + 1) continue;
        std::swap(a[i], a[++k]);
        for (int i = 1; i <= n; i++) {
            if (i == k) continue;
            if ((a[i] >> j) & 1) a[i] ^= a[k];
        }
    }
    zero = (k != n);
    n = k;
}

// find kth smallest xor
ll query(ll kth)
{
    ll ret = 0; kth -= zero;
    for (int i = n; i; i--, kth /= 2)
        if (kth & 1) ret ^= a[i];
    // no solution
    if (kth) return -1;
    return ret;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    int T; std::cin >> T;
    for (int ti = 1; ti <= T; ti++) {
        std::cout << "Case #" << ti << ":\n";
        std::cin >> n;
        for (int i = 1; i <= n; i++) std::cin >> a[i];
		gaussian_elimination();
        int q; std::cin >> q;
        for (int i = 0; i < q; i++ ) {
            ll x; std::cin >> x;
            std::cout << query(x) << "\n";
        }
	}
}

